package cn.xfdzx.android.apps.shop.fragment.type;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.tools.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.HomeSearchActivity;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.adapter.MarketLeftMenuAdapter;
import cn.xfdzx.android.apps.shop.adapter.MarketRightRecyerAdapter;
import cn.xfdzx.android.apps.shop.adapter.MarketTopRecyerAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.MvpBaseFragment;
import cn.xfdzx.android.apps.shop.bean.CategoryAllBeen;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.evenbus.HomeFlushCarData;
import cn.xfdzx.android.apps.shop.evenbus.UpdateCarNumMessage;
import cn.xfdzx.android.apps.shop.fragment.type.contract.MarketClassificationContract;
import cn.xfdzx.android.apps.shop.fragment.type.presenter.MarketClassificationPresenter;
import cn.xfdzx.android.apps.shop.fragment.type.request.AddCart;
import cn.xfdzx.android.apps.shop.fragment.type.request.CateAll;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;


public class MarketClassificationFragment extends MvpBaseFragment<MarketClassificationPresenter> implements MarketClassificationContract.View {

    @BindView(R.id.top_recyer)
    RecyclerView topRecyer;
    @BindView(R.id.left_meau)
    RecyclerView leftMeau;
    @BindView(R.id.right_recyer)
    RecyclerView rightRecyer;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    private MarketTopRecyerAdapter topRecyerAdapter;  //顶部列表选项

    private RelativeLayout.LayoutParams item_bar;
    private MarketLeftMenuAdapter marketLeftMenuAdapter;//左侧列表选项
    private MarketRightRecyerAdapter adapter;

    int p = 0;//当前顶部列表选择的下标
    int leftPostion = 0;//当前左侧选择的下标
    private List<CategoryAllBeen.DataBean> list;

    @Override
    public MarketClassificationPresenter createPresenter() {
        return new MarketClassificationPresenter();
    }

    @Override
    protected void initView(View view) {

        EventBus.getDefault().register(this);
        mPresenter.getClassoficationData("2");
        //顶部列表
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false);
        topRecyer.setLayoutManager(linearLayoutManager);
        topRecyerAdapter = new MarketTopRecyerAdapter();
        topRecyer.setAdapter(topRecyerAdapter);

        //左侧列表
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false);
        marketLeftMenuAdapter = new MarketLeftMenuAdapter();
        leftMeau.setLayoutManager(layoutManager);
        leftMeau.setAdapter(marketLeftMenuAdapter);

        //右侧列表
        View empey_no_goods = View.inflate(getActivity(), R.layout.empey_no_goods, null);
        adapter = new MarketRightRecyerAdapter();
        rightRecyer.setLayoutManager(new LinearLayoutManager(mContext));
        rightRecyer.setAdapter(adapter);
        adapter.setEmptyView(empey_no_goods);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<HomeSearchXFDGoodBeen.DataBean> data = adapter.getData();
                Intent intent = new Intent(getContext(), XFDGoodsDeatailsActivity.class);
                intent.putExtra("good_id", data.get(position).getId() + "");
                startActivity(intent);
            }
        });

        //顶部item点击
        topRecyerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<CategoryAllBeen.DataBean> data = adapter.getData();
                marketLeftMenuAdapter.setNewData(data.get(position).getChild());
                marketLeftMenuAdapter.selItem(0);
                topRecyerAdapter.setColor(position);
                getGoodsList(data.get(position).getChild().get(0).getId());

                p = position;
                leftPostion = 0;

            }
        });

        //左侧item 点击事件
        marketLeftMenuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                marketLeftMenuAdapter.selItem(position);
                List<CategoryAllBeen.DataBean.ChildBean> data = adapter.getData();
                getGoodsList(data.get(position).getId());

                leftPostion = position;
            }
        });


        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<HomeSearchXFDGoodBeen.DataBean> data = adapter.getData();
                AddCart addCart = new AddCart();
                addCart.setIs_new("0");
                addCart.setCartNum("1");
                addCart.setProductId(data.get(position).getId() + "");
                mPresenter.addCart(addCart, position);

            }
        });


    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_market_classification;
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void getSuccessData(CategoryAllBeen categoryAllBeen) {

        if (categoryAllBeen.getStatus() == Constants.SUCCESS_CODE) {

            list = categoryAllBeen.getData();
            topRecyerAdapter.setNewData(categoryAllBeen.getData());
            try {
                marketLeftMenuAdapter.setNewData(categoryAllBeen.getData().get(0).getChild());
                getGoodsList(categoryAllBeen.getData().get(0).getChild().get(0).getId());
            } catch (Exception e) {

            }

        }
    }


    /**
     * 获取右侧分类数据列表
     *
     * @param id
     */
    private void getGoodsList(int id) {

        CateAll cateAll = new CateAll();
        cateAll.setSid(id + "");
        cateAll.setPages(1);
        cateAll.setLimit(20);
        cateAll.setType("1");
        cateAll.setIndex_status("1");
        cateAll.setIs_store("0");
        mPresenter.getListAll(cateAll);
    }

    @Override
    public void getListGoodsSuccess(HomeSearchXFDGoodBeen goodBeen) {
        adapter.setNewData(goodBeen.getData());

    }

    @Override
    public void getOnFail(String fail) {

        ToastUtils.s(mContext, fail);
    }

    @Override
    public void addCarSuccess(ConfirmGoodBeen goodBeen, int postion) {
        if (goodBeen.getStatus() == Constants.SUCCESS_CODE) {
//            List<HomeSearchXFDGoodBeen.DataBean> data = adapter.getData();
//            data.get(postion).setCart_num(data.get(postion).getCart_num() + 1);
//            adapter.notifyItemChanged(postion);
            Toast.makeText(getActivity(), "加入购物车成功", Toast.LENGTH_SHORT).show();
            UtilsData.getPreference(getActivity(), "carCount").put("car_pf_count", goodBeen.getData().getCount().getPfcount());
            EventBus.getDefault().post(new UpdateCarNumMessage());
            EventBus.getDefault().post(new HomeFlushCarData());

        } else {


            if (goodBeen.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(getActivity(), "login").remove("head_url");
                UtilsData.getPreference(getActivity(), "login").remove("nickname");
                UtilsData.getPreference(getActivity(), "login").remove("is_store");
                UtilsData.getPreference(getActivity(), "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(getActivity(), OneLoginMainActivity.class);
                startActivity(intent);
                return;
            } else {
                Toast.makeText(getActivity(), goodBeen.getMsg(), Toast.LENGTH_SHORT).show();
            }

        }

    }

    //购物车数据改变刷新首页数据  购物车角标数量展示
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void flushHomeCarData(HomeFlushCarData messageEvent) {

        getGoodsList(list.get(p).getChild().get(leftPostion).getId());

    }

    @OnClick(R.id.iv_search)
    public void onViewClicked() {


        if (Utils.isFastClick()) {
            Intent home_search = new Intent(getActivity(), HomeSearchActivity.class);
            startActivity(home_search);
        }
    }
}
